package com.dacd.pdf.activity;

import android.graphics.Bitmap;
import android.graphics.pdf.PdfRenderer;
import android.os.Bundle;
import android.os.ParcelFileDescriptor;
import android.support.annotation.Nullable;
import android.widget.ImageView;

import com.dacd.filehelper.FileConstant;
import com.dacd.filehelper.FileUtils;
import com.dacd.pdf.R;

import java.io.File;
import java.io.IOException;

public class ShowEditedPdfActivity extends BaseActivity {

    private ParcelFileDescriptor mFileDescriptor;// File descriptor of the PDF.
    private PdfRenderer mPdfRenderer;
    private PdfRenderer.Page mCurrentPage;//Page that is currently shown on the screen.
    private ImageView showEditedIv;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentLayout(R.layout.activity_show_edite_pdf);
        showEditedIv = (ImageView) findViewById(R.id.asep_iv);
        showPdf();
    }

    private void showPdf() {
        String sdPath = FileUtils.getSDPath();
        if (sdPath == null) {//没有SD卡不能保存，可以添加提示
            return;
        }
        final File folderFile = new File(sdPath + FileConstant.PDF_PATH);
        if (!folderFile.exists() || !folderFile.isDirectory()) {
            return;
        }
        final File[] allPdfFiles = folderFile.listFiles();
        if(allPdfFiles == null || allPdfFiles.length < 1) {
            return;
        }
        String fileName = getIntent().getStringExtra("fileName");
        File pdfFile = null;
        for(File file : allPdfFiles) {
            if(file.getName().equals(fileName)) {
                pdfFile = file;
            }
        }
        if(pdfFile == null) {
            return;
        }
        try {
            openRenderer(pdfFile.getAbsolutePath());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    /**
     * Sets up a {@link android.graphics.pdf.PdfRenderer} and related resources.
     */
    private void openRenderer(String path) throws IOException {
        // In this sample, we read a PDF from the assets directory.
        File file = new File(path);
        mFileDescriptor = ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_ONLY);
        // This is the PdfRenderer we use to render the PDF.
        if (mFileDescriptor != null) {
            mPdfRenderer = new PdfRenderer(mFileDescriptor);
        }
        showPage(0);
    }

    /**
     * Shows the specified page of PDF to the screen.
     *
     * @param index The page index.
     */
    private void showPage(int index) {
        if (mPdfRenderer.getPageCount() <= index) {
            return;
        }
        // Make sure to close the current page before opening another one.
        if (null != mCurrentPage) {
            mCurrentPage.close();
        }
        // Use `openPage` to open a specific page in PDF.
        mCurrentPage = mPdfRenderer.openPage(index);
        // Important: the destination bitmap must be ARGB (not RGB).
        Bitmap bitmap = Bitmap.createBitmap(mCurrentPage.getWidth(), mCurrentPage.getHeight(),
                Bitmap.Config.ARGB_8888);
        // Here, we render the page onto the Bitmap.
        // To render a portion of the page, use the second and third parameter. Pass nulls to get
        // the default result.
        // Pass either RENDER_MODE_FOR_DISPLAY or RENDER_MODE_FOR_PRINT for the last parameter.
        mCurrentPage.render(bitmap, null, null, PdfRenderer.Page.RENDER_MODE_FOR_DISPLAY);
        // We are ready to show the Bitmap to user.
        showEditedIv.setImageBitmap(bitmap);
    }
}
